﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class LoadManager : Singleton<LoadManager> {

#region UI控件
    [SerializeField, Tooltip("获取加载界面")]
    public GameObject loadPanel;
    [SerializeField, Tooltip("获取加载的Slider")]
    private Slider slider;
    [SerializeField, Tooltip("获取显示进度条的值")]
    private Text textValue;
#endregion
    public override void FixedAwake()
    {
    }
    public void Start()
    {
        loadPanel = GameObject.Find("Canvas/LoadPanel");
        if (loadPanel != null)
        {
            slider = loadPanel.transform.GetChild(0).GetComponent<Slider>();//获取加载条
            textValue = loadPanel.transform.GetChild(1).GetComponent<Text>();//显示加载进度
            loadPanel.SetActive(false);//隐藏掉加载界面
        }
    }
    public void Loading(int index)
    {
        if (MusicManager.Instance != null) { MusicManager.Instance.VoiceChange(MusicOrSound.Music, false); }//关闭音乐
        StartCoroutine(GetProgress(index));
    }
    /// <summary>
    /// 加载条进度
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    private IEnumerator GetProgress(int index)
    {
        AsyncOperation async = SceneManager.LoadSceneAsync(index);
        loadPanel.SetActive(true);
        while (async.isDone == false)
        {
            double progress =Math.Round( Mathf.Clamp01(async.progress / 0.9f),2);
            slider.value = (float)progress;
            textValue.text = (progress * 100).ToString() + "%";
            yield return null;
        }
    }
}
